home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / we-30d.zip / WEEXTSRC.ZI_ / CTAGS.C next >
C/C++ Source or Header  |  1992-09-21  |  7KB  |  224 lines

  1. /*------------------------------------------------------------------------*
  2.  * CTAGS.C   - sample source for a GoToTag function.  To use this         *
  3.  *             function, add a command/menu mapping to WE_EXT.RC, add     *
  4.  *             a case statement for the new command, and compile and      *
  5.  *             link this file with WE_EXT.C.                              *
  6.  *                                                                        *
  7.  *             The TAGS.EXE program shipped with WinEdit generates a      *
  8.  *             tag file in the correct format with the following          *
  9.  *             command line:                                              *
  10.  *                                                                        *
  11.  *                 TAGS -om -tWINEDIT.MRK filespec                        *
  12.  *                                                                        *
  13.  *------------------------------------------------------------------------*/
  14.  
  15. #define STRICT
  16. #define _WINDLL
  17. #include <windows.h>
  18. #include "we_ext.h"
  19. #include "private.h"
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25.  
  26. char gszTemp[16];      /* this needs to be in DGROUP for atoi()  */
  27. char gszFileName[256]; /* this needs to be in DGROUP for fopen() */
  28.  
  29. #define BUFSIZE 16383
  30.  
  31. /*------------------------------------------------------------------------*
  32.  * GoToTag() - retrieves the word at the caret and looks up the           *
  33.  *             tag in WINEDIT.MRK.  If found, loads the appropriate       *
  34.  *             file and goes to that line.                                *
  35.  *                                                                        *
  36.  * RETURNS:    Nothing.                                                   *
  37.  *                                                                        *
  38.  *------------------------------------------------------------------------*/
  39. void GoToTag(HWND hWnd)
  40.    {
  41.    char szTemp[64];
  42.    char szLoadFile[256];
  43.    UINT uiLineNo;
  44.  
  45.    edEditGetCurrentWord(hWnd,szTemp,63);
  46.  
  47.    lstrcpy(gszFileName,"WINEDIT.MRK");
  48.    uiLineNo = FindToken(szTemp,szLoadFile);
  49.  
  50.    if (uiLineNo)
  51.       {
  52.       if (edFileOpen(hWnd,szLoadFile))
  53.          edEditGoToLine(hWnd,uiLineNo);
  54.       }
  55.    }
  56.  
  57.  
  58. /*------------------------------------------------------------------------*
  59.  *                                                                        *
  60.  * FindToken() - find the token lpToken in lpTagFileName.                 *
  61.  *                                                                        *
  62.  * RETURNS:     Line number if found.  0 if not.                          *
  63.  *                                                                        *
  64.  * The tag file must be in Microsoft format: TOKEN FILENAME(lineno).      *
  65.  *                                                                        *
  66.  * The TAGS.EXE program shipped with WinEdit generates a tag file         *
  67.  * in the correct format with the following command line:                 *
  68.  *                                                                        *
  69.  *          TAGS -om -tWINEDIT.MRK filespec                               *
  70.  *                                                                        *
  71.  *------------------------------------------------------------------------*/
  72. UINT FindToken(LPSTR szToken, LPSTR lpLoadFileName)
  73.    {
  74.    int f;
  75.    int i;
  76.    char szNewFile[256];
  77.    UINT uiLineNo,uiRval;
  78.    OFSTRUCT of;
  79.    HANDLE hBuffer;
  80.    LPSTR  lpBuffer;
  81.    LPSTR lpFoundIt;
  82.    LONG lFilePos;
  83.    
  84.    uiLineNo = 0;
  85.    f = OpenFile(gszFileName,&of,OF_READ|OF_SHARE_DENY_NONE);
  86.    if (f == HFILE_ERROR)
  87.       return 0;
  88.  
  89.    _fstrupr(szToken);
  90.    
  91.    hBuffer = GlobalAlloc(GHND,BUFSIZE+1);
  92.    if (!hBuffer)
  93.       return 0;
  94.       
  95.    lpBuffer = GlobalLock(hBuffer);   
  96.    
  97.    uiRval = _lread(f,lpBuffer,BUFSIZE);
  98.    
  99.    _fstrupr(lpBuffer);
  100.  
  101.    /* back up to the last CR/LF */
  102.    if (uiRval)
  103.       i = uiRval-1;
  104.    else
  105.       i = 0;
  106.    while (i && (lpBuffer[i] != 0x0D))
  107.       i--;
  108.    if (i)
  109.       {
  110.       i += 2;
  111.       _llseek(f,i,0);
  112.       lFilePos = uiRval = (UINT)i;
  113.       }
  114.  
  115.    while (uiRval)
  116.       {
  117.       lpFoundIt = Scan(szToken,
  118.                        lpBuffer,
  119.                        uiRval);
  120.                        
  121.       if (lpFoundIt)
  122.          {
  123.          /* move to the file name */                                          
  124.          for (;*lpFoundIt != ' ';lpFoundIt++);                                
  125.  
  126.          lpFoundIt++;
  127.  
  128.          /* build the file name */                                            
  129.          for (i=0;(*lpFoundIt != ' ') && (*lpFoundIt != '(');i++,lpFoundIt++) 
  130.             szNewFile[i] = *lpFoundIt;                                        
  131.  
  132.          szNewFile[i] = 0;
  133.  
  134.          lpFoundIt++;                                                         
  135.  
  136.          /* grab the line number */
  137.          for (i=0;(*lpFoundIt != ' ') && (*lpFoundIt != ')');i++,lpFoundIt++)
  138.             gszTemp[i] = *lpFoundIt;
  139.  
  140.          gszTemp[i] = 0;
  141.  
  142.          uiLineNo = atoi(gszTemp);
  143.          break;
  144.          }
  145.       else
  146.          {
  147.          uiRval = _lread(f,lpBuffer,BUFSIZE);
  148.  
  149.          if (uiRval)
  150.             {
  151.             _fstrupr(lpBuffer);
  152.  
  153.             /* back up to the last CR/LF */
  154.             i = uiRval-1;
  155.             while (i && (lpBuffer[i] != 0x0D))
  156.                i--;
  157.             if (i)
  158.                {
  159.                i += 2;
  160.                lFilePos += i;
  161.                _llseek(f,lFilePos,0);
  162.                uiRval = (UINT)i;
  163.                }
  164.             }
  165.          }
  166.       }
  167.  
  168.  
  169.    _lclose(f);
  170.  
  171.    lstrcpy(lpLoadFileName,szNewFile);
  172.  
  173.    return uiLineNo;
  174.    }
  175.    
  176.  
  177. /*------------------------------------------------------------------------*
  178.  * Scan()    - brute force buffer searcher.                               *
  179.  *                                                                        *
  180.  * RETURNS:    Pointer to matching string, NULL if not found.             *
  181.  *                                                                        *
  182.  *------------------------------------------------------------------------*/
  183.  
  184. LPSTR Scan(LPSTR lpPattern,
  185.              LPSTR lpText,
  186.              int   iLength)
  187.    {
  188.    LPSTR lpNext;
  189.    LPSTR lpFound;
  190.    LPSTR lpTemp;
  191.    LPSTR lpCheckText;
  192.    char cFirstChar;
  193.  
  194.    cFirstChar = lpPattern[0];
  195.    lpTemp = lpText;
  196.  
  197.    /* search for the first character */
  198.    lpFound = lpNext = _fmemchr(lpTemp,cFirstChar,iLength);
  199.  
  200.    while (lpNext)
  201.       {
  202.       /* check for a complete match */
  203.       for (lpCheckText = lpPattern;
  204.            *lpCheckText && *lpNext && (*lpCheckText == *lpNext);
  205.            *lpCheckText++,*lpNext++);
  206.  
  207.       if (!(*lpCheckText))
  208.          /* we got a complete match */
  209.          return lpFound;
  210.  
  211.       iLength -= (lpNext - lpTemp);
  212.  
  213.       lpTemp = lpNext;
  214.  
  215.       /* search for the next occurence of the first character */
  216.       if (iLength > 0)
  217.          lpFound = lpNext = _fmemchr(lpTemp,cFirstChar,iLength);
  218.       else
  219.          break;
  220.       }
  221.  
  222.    return NULL;
  223.    }
  224.